import wifi from '@ohos.wifi';
import router from '@system.router';
import KvStoreModel from '../../model/kv.js';
import socket from '@ohos.net.socket';
import {getPrescritionList} from '../../common/utils/getPrescriptionList';

const CHANGE_PRESCRTION = "prescription";
export default {
    data: {
        picArr:[1,1,1],
        //药单数据
        isSendingList:[],//配送中药单数据
        unTreatedList:[], //未处理药单数据
        deliveredList:[],//已送达药单数据

        kvStoreModel:new KvStoreModel(),
        newPrescription:null,
        ttt:"123",
        TAG:"YZJ: ",
        localip:"",
        remoteip:"192.168.1.101",
        tcp:socket.constructTCPSocketInstance(),
        state1:"未处理",
        stateFlag1:0,//0表示没有未处理药单，1表示有
        state2:"配送中",
        stateFlag2:0,//0表示没有配送中药单，1表示有
        state3:"已送达",
        stateFlag3:0,//0表示没有已送达药单，1表示有
        showInfo:false,
        color1:"#0a59f7",
        color2:"#6f6162",
        title: "",
        tabInfo:[{
                     picPath:"tab1On.png",
                     title:"未处理",
                 },{
                     picPath:"tab2Off.png",
                     title:"已处理",
                 }],
        sendor_not:1,
    },
    onInit() {
        this.creatScoket();

        this.onShow();
    },
    onShow(){
        getPrescritionList("配送中",(res)=>{
            this.isSendingList = res;
            console.log("xxx---配送中："+this.isSendingList[0].state)
            //订单分类是否为空
            this.orderClassification();
        })
        getPrescritionList("未处理",(res)=>{
            this.unTreatedList = res;
            console.log("xxx---未处理："+this.unTreatedList[0].basic_info.bed_number)
            //订单分类是否为空
            this.orderClassification();
        })
        getPrescritionList("已送达",(res)=>{
            this.deliveredList = res;
            console.log("xxx---已送达："+this.deliveredList)
            //订单分类是否为空
            this.orderClassification();
        })



//        if(this.sendor_not==1){
//            this.sendMessage();
//        }

    },
    async creatScoket(){
        var that = this;
        this.getIpAddress();
        //绑定本机主机
        this.tcp.bind({address:this.localip, port:8080, family: 1}, err => {
            if (err) {
                console.info(this.TAG+'bind fail');
                this.title='bind fail'
                return;
            }
            this.title='bind success'
            console.info(this.TAG+'bind success');
        })
        //监听信息
        this.tcp.on('message', value => {
            let buffer = value.message;
            let dataView = new DataView(buffer);
            let str = "";

            for (let i = 0;i < dataView.byteLength; ++i) {
                str += String.fromCharCode(dataView.getUint8(i))

            }
            console.info(this.TAG+"on connect recevied"+str );

        });
    },
    //发送信息
    sendMessage(){
        let promise = this.tcp.connect({ address: {address: this.remoteip, port: 8080, family: 1} , timeout: 6000});
        promise.then(() => {
            console.log(this.TAG+'connect success');
            this.tcp.send({
                data:'101->102->103'
            },err => {
                if (err) {
                    console.log(this.TAG+'send fail');
                    return;
                }
                console.log(this.TAG+'send success');
            })
        }).catch(err => {
            console.log(this.TAG+'connect fail');
        });
    },
    uni2str(str){
        var result = [];
        var res="";
        var strArr = str.split('\\u');//根据 \u 分割
        var len = strArr.length;
        for(var i=0; i<len; i++){
            if(strArr[i]){

                result.push(String.fromCharCode(parseInt(strArr[i], 16)));//16 转 10
                res+=String.fromCharCode(parseInt(strArr[i], 16));
            }
        }
        return result.join('');
        //  return res;
    },
    getIpAddress(){
        let ip=wifi.getIpInfo().ipAddress;
        this.localip = (ip >> 24 & 0xFF)+"."+ ((ip >> 16) & 0xFF)+"."+((ip >> 8) & 0xFF)+"."+(ip & 0xFF);
        console.info(this.TAG+this.localip);
    },
    //tabbar跳转
    change(e){
        console.log(e.index);
        if(e.index==1){
            this.tabInfo[0].picPath="tab1Off.png";
            this.tabInfo[1].picPath="tab2On.png";
        }
        else if(e.index==0){
            this.tabInfo[0].picPath="tab1On.png";
            this.tabInfo[1].picPath="tab2Off.png";
        }
    },
    toMessagePage(){
        router.push({
            uri: 'pages/messagePage/messagePage',
            //            params: {
            //                data1: 'message',
            //                data2: {
            //                    data3: [123, 456, 789]
            //                },
            //            },
        });
    },

    //查看药单并且获取当前药单信息
    toCheckMedList(e){
        console.log("xxx---toCheckMedList");
        let that = this;
        let curIndex=e.target.dataSet.index;
        let state = e.target.dataSet.flag;
        console.log("xxx---flag:"+state+"   index:"+curIndex);
        let curPrescription;
        if(state=="未处理"){
            for (let i = 0; i < that.unTreatedList.length; i++) {
                if(that.unTreatedList[i].ID==curIndex){
                    curPrescription = that.unTreatedList[i];
                }
            }
            router.push({
                uri: 'pages/checkMedList/checkMedList',
                params: {
                    curPrescription:curPrescription
                },
            });
        }
        else if(state=="配送中"){
            for (let i = 0; i < that.isSendingList.length; i++) {
                if(that.isSendingList[i].ID==curIndex){
                    curPrescription = that.isSendingList[i];
                }
            }
            router.push({
                uri: 'pages/checkMedList/checkMedList',
                params: {
                    curPrescription:curPrescription
                },
            });
        }
        else if(state=="已送达"){
            for (let i = 0; i < that.deliveredList.length; i++) {
                if(that.deliveredList[i].ID==curIndex){
                    curPrescription = that.deliveredList[i];
                }
            }
            router.push({
                uri: 'pages/checkMedList/checkMedList',
                params: {
                    curPrescription:curPrescription
                },
            });
        }


    },


    //回到首页
    toIndex(){
        router.push({
            uri: 'pages/login/login',
            //            params: {
            //                data1: 'message',
            //                data2: {
            //                    data3: [123, 456, 789]
            //                },
            //            },
        });
    },
    //swiper页面
    toDelivering(){
        this.$element('swiper').swipeTo({index: 0});
        this.color1="#0a59f7";
        this.color2="#6f6162";
    },
    toFinish(){
        this.$element('swiper').swipeTo({index: 1});
        this.color2="#0a59f7";
        this.color1="#6f6162";
    },
    //监听订单分类是否为空
    orderClassification(){
        var that = this;
        console.log("xxx---unTrea length:"+this.unTreatedList.length)
        if(this.unTreatedList.length != 0)
        that.stateFlag1=1;
        else
        that.stateFlag1=0;
        console.log("xxx---isSendingList length:"+this.isSendingList.length)
        if(this.isSendingList.length != 0)
        that.stateFlag2=1;
        else
        that.stateFlag2=0;
        console.log("xxx---deliveredList length:"+this.deliveredList.length)
        if(this.deliveredList.length != 0)
        that.stateFlag3=1;
        else
        that.stateFlag3=0;
    },

}
